package com.fengmi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.fengmi.dto.GoodsDTO;
import com.fengmi.dto.PageDTO;
import com.fengmi.entity.MallGoods;
import com.fengmi.entity.MallGoodsBrand;
import com.fengmi.entity.MallGoodsSku;
import com.fengmi.entity.SysUser;
import com.fengmi.mapper.GoodsMapper;
import com.fengmi.mapper.GoodsSkuMapper;
import com.fengmi.mapper.MallGoodsMapper;
import com.fengmi.service.GoodsService;
import com.fengmi.view.ViewGoodsInfo;
import com.fengmi.vo.PageResultVO;
import com.fengmi.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional()
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private MallGoodsMapper mallGoodsMapper;


    @Override
    public PageResultVO<MallGoods> findGoodsByPageAndWhere(PageDTO<MallGoods> pageDTO) {
        //非空判断
        if (pageDTO==null){
            return new PageResultVO<>(false,"参数不合法");
        }

        //当前页
        Integer pageNum = pageDTO.getPageNum();
        //每页记录数
        Integer pageSize = pageDTO.getPageSize();
        //进行非空判断
        if (pageNum == null || pageNum<=0){
            pageNum = 1;
        }
        if (pageSize == null || pageSize<=0){
            pageSize = 5;
        }
        //开始分页
        PageHelper.startPage(pageNum,pageSize);
        //调用mapper层查询
        List<MallGoods> goodsList = mallGoodsMapper.findGoodsByWhere(pageDTO.getWhere());
        //分页信息对象创建，放入查询集合
        PageInfo<MallGoods> pageInfo = new PageInfo<>(goodsList);
        //创建返回对象
        PageResultVO<MallGoods> pageResultVO = new PageResultVO<>(true,"查询成功");
        //设置每页数据
        pageResultVO.setData(pageInfo.getList());
        //设置总页数
        pageResultVO.setTotalPage(pageInfo.getPages());
        //设置总记录数
        pageResultVO.setTotalSize(pageInfo.getTotal());

        return pageResultVO;
    }

    @Override
    public PageResultVO<ViewGoodsInfo> findGoodsByPage(PageDTO<MallGoods> pageDTO) {

        //非空判断
        if (pageDTO==null){
            return new PageResultVO<>(false,"参数不合法");
        }

        //当前页
        Integer pageNum = pageDTO.getPageNum();
        //每页记录数
        Integer pageSize = pageDTO.getPageSize();
        //进行非空判断
        if (pageNum == null || pageNum<=0){
            pageNum = 1;
        }
        if (pageSize == null || pageSize<=0){
            pageSize = 5;
        }


        PageHelper.startPage(pageNum,pageSize);

        PageInfo<ViewGoodsInfo> pageInfo = new PageInfo<>(mallGoodsMapper.findGoodsByPage(pageDTO.getWhere()));



        //创建返回对象
        PageResultVO<ViewGoodsInfo> pageResultVO = new PageResultVO<>(true,"查询成功");
        //设置每页数据
        pageResultVO.setData(pageInfo.getList());
        //设置总页数
        pageResultVO.setTotalPage(pageInfo.getPages());
        //设置总记录数
        pageResultVO.setTotalSize(pageInfo.getTotal());



        return pageResultVO;
    }

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodsSkuMapper goodsSkuMapper;

    @Override
    public ResultVO save(GoodsDTO goodsDTO) {

        if (goodsDTO == null) {
            return new ResultVO(false,"非法参数");
        }

        if (goodsDTO.getGoods() == null) {
            return new ResultVO(false,"非法参数");

        }
        if(goodsDTO.getSkus() == null || goodsDTO.getSkus().size() <=0)
            return new ResultVO(false,"非法参数");


        //保存spu信息
        MallGoods goods = goodsDTO.getGoods();
        goods.setCreateTime(new Date());
        goods.setUpdateTime(new Date());
        goods.setAuditStatus("0");//状态 0：未审核 1：审核通过 2：审核不通过
//        goods.setDefaultSkuId();
        goods.setIsDelete("0");
        //springsecurity的context中获取用户的主体信息
        SysUser principal = (SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        goods.setUserId(principal.getUserId()+"");
        goodsMapper.insertGoods(goods);



        //添加sku
        List<MallGoodsSku> skus = goodsDTO.getSkus();
        skus.forEach(sku->{
            sku.setCreateTime(new Date());
            sku.setUpdateTime(new Date());
            sku.setSpuId(goods.getSpuId());
            goodsSkuMapper.insertSku(sku);
        });


        return new ResultVO(true,"商品保存成功");
    }
}
